import SparkMD5 from 'spark-md5'

self.onmessage = (e) => {
  const { file } = e.data
  const hash = new SparkMD5.ArrayBuffer() // 构建hash值对象
  const total = file.length

  // 读取文件
  const reader = new FileReader()
  // 指定读取的文件
  reader.readAsArrayBuffer(file)

  reader.onload = (e) => {
    console.log(e)
    hash.append(reader.result as ArrayBuffer)
    self.postMessage({ type: 'progress' })
  }

  reader.onerror = (error) => {
    self.postMessage({ type: 'error', error })
  }

  const result = hash.end()
  self.postMessage({ type: 'success', hash: result })
}
